# -*- coding: utf-8 -*-
from openerp.osv import fields
from openerp.osv import osv
from openerp import SUPERUSER_ID
import datetime, time, pytz
import openerp.addons.decimal_precision as dp
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, float_compare
from openerp.http import request
from openerp.tools import ustr
from openerp.fields import Datetime, Field
ISODATEFORMAT = '%Y-%m-%d'

Newdatetime = Datetime.convert_to_export


def convert_to_export(self, value, env):
    timezone = pytz.timezone(env.context.get('tz'))
    return_val = Newdatetime(self, value, env)
    if isinstance(return_val, datetime.datetime) and return_val:
        return self.to_string(return_val.replace(tzinfo=pytz.timezone('UTC')).astimezone(timezone))
    elif return_val:
        return self.to_string(self.from_string(return_val).replace(tzinfo=pytz.timezone('UTC')).astimezone(timezone))
    return return_val
Datetime.convert_to_export = convert_to_export

class weixin_approval(osv.osv):
    _name = 'weixin.approval'
    _columns = {
        'apply_name': fields.char(u'申请人姓名'),
        'apply_org': fields.char(u'申请人部门'),
        'approval_name': fields.char(u'审批人姓名'),
        'notify_name': fields.char(u'抄送人姓名'),
        'sp_status': fields.selection([(1, u'审批中'), (2, u'已通过'), (3, u'已驳回'), (4, u'已取消')], u'审批状态'),
        'sp_num': fields.char(u'审批单号'),

        'apply_time': fields.char(u'审批单提交时间'),
        'apply_datetime': fields.datetime(u'审批单提交时间'),
        'apply_user_id': fields.many2one('weixin.contacts', u'审批单提交者的userid'),
        'leave_type': fields.selection([(1, u'年假'), (2, u'事假'),
                                        (3, u'病假'), (4, u'调休假'),
                                        (5, u'婚假'), (6, u'产假'),
                                        (7, u'陪产假'), (8, u'其他'),
                                        (9, u'补卡'), (10, u'外出')], u'请假类型'),
        'start_time': fields.char(u'请假开始时间'),
        'end_time': fields.char(u'请假结束时间'),
        'start_datetime': fields.datetime(u'开始时间'),
        'end_datetime': fields.datetime(u'结束时间'),
        'duration': fields.char(u'请假时长，单位小时'),
        'reason': fields.char(u'请假事由'),
        'active': fields.boolean(u'可见'),
        'comm': fields.text(u'组件信息'),
        'mediaids': fields.char(u'微信审批附件'),
        'spname': fields.char(u'审批单据类型')
    }
    _defaults = {
        'active': True
    }

    def change_unix_to_date_time(self, cr, uid, unix_time, context=None):
        """把utc 时间转换成本地时间"""
        return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(unix_time)))

    def create(self, cr, uid, vals, context=None):
        """在创建的时候要对时间进行转换，因为微信传过来的是时间戳"""
        if vals.get('start_time'):
            vals.update(
                {'start_datetime': self.change_unix_to_date_time(cr, uid, vals.get('start_time'), context=context)})
        if vals.get('end_time'):
            vals.update({'end_datetime': self.change_unix_to_date_time(cr, uid, vals.get('end_time'), context=context)})
        if vals.get('apply_time'):
            vals.update(
                {'apply_datetime': self.change_unix_to_date_time(cr, uid, vals.get('apply_time'), context=context)})
        return super(weixin_approval, self).create(cr, uid, vals, context=context)
